﻿Imports System.Windows.Forms
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Controls

Public Class Main

    Dim welcome As WelcomeForm
    Public workspaces As Dictionary(Of String, Workspace) = New Dictionary(Of String, Workspace)

    Public Sub initProperties(ByVal userName As String)
        connectedUser.Text = "Utilisateur :" & userName
        connectedDate.Text = "               Connecté le : " & Date.Now
    End Sub

   
    Public Sub openWorkspace(ByVal codeSite As String)
        Me.Cursor = Cursors.WaitCursor
        Dim workspace As New Workspace()
        workspace.configPath = getSiteConfigFilePath(codeSite)
        workspace.MdiParent = Me
        workspace.Text = "Espace de travail du site : " & codeSite
        workspace.WindowState = FormWindowState.Maximized
        workspace.mode = 2
        workspace.removed = False
        workspaces.Add(codeSite, workspace)
        workspace.Show()
        workspace.BringToFront()
        welcome.SendToBack()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub


    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private m_ChildFormNumber As Integer

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim userDAO As UserDAO = New UserDAO
        Dim actionsList = userDAO.getUserActions(Settings.Default.CurrentUser)
        Dim canConfig As Boolean = False
        For Each action In actionsList
            If action.action_name = Actions.config_action Then
                canConfig = True
                Exit For
            End If
        Next
        ConfigurationToolStripMenuItem.Visible = canConfig

        Timer1.Interval = 1000
        Timer1.Start()
        welcome = New WelcomeForm()
        welcome.actionsList = actionsList
        AddHandler welcome.Activated, AddressOf OnWelcomFormShow
        welcome.ControlBox = False
        welcome.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog

      
        welcome.MdiParent = Me
        welcome.WindowState = FormWindowState.Maximized
        welcome.Show()

        'Dim workspace As New Workspace()
        'workspace.configPath = "D:\samples\ws1\altaf.silm"
        'workspace.MdiParent = Me
        'workspace.WindowState = FormWindowState.Maximized
        'workspace.mode = 2
        'workspace.Show()
    End Sub
    Private Sub dragFormEvent(ByVal sender As Object, ByVal e As EventArgs)
        If sender IsNot Nothing And TryCast(sender, Form) IsNot Nothing Then
            TryCast(sender, Form).Location = New System.Drawing.Point(0, 0)
        End If
    End Sub
    Private Sub AProposToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AProposToolStripMenuItem.Click
        Dim about As AboutSilm = New AboutSilm()
        about.ShowDialog(Me)
    End Sub



    Private Sub Main_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Application.Exit()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        currentDate.Text = "             " & System.DateTime.Now
    End Sub

    Private Sub OnWelcomFormShow(ByVal sender As Object, ByVal e As System.EventArgs)
        welcome.displayConfiguredSites()
    End Sub

    Private Sub HelpMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim form As New Form2
        form.ShowDialog(Me)
    End Sub

    Private Sub DémarrageDeLapplicationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DémarrageDeLapplicationToolStripMenuItem.Click
        Dim startupFrm As AppStartup = New AppStartup()
        startupFrm.ShowDialog(welcome)
    End Sub

    Private Sub EspaceDeTravailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EspaceDeTravailToolStripMenuItem.Click
        Dim workSpaceConfig As New WorkspacesConfig
        workSpaceConfig.ShowDialog(welcome)
    End Sub

    Private Sub CouchesDisponiblesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CouchesDisponiblesToolStripMenuItem.Click
        Dim configLayers As New layersSiteConfigUI()
        configLayers.ShowDialog(welcome)
    End Sub
End Class
